package com.example.hm_dtfkz.ui.fragment;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import androidx.viewbinding.ViewBinding;

import com.android.base.ui.base.VBBaseFragment;
import com.android.base.util.http.RxhttpUtil;
import com.example.hm_dtfkz.MainActivity;
import com.example.hm_dtfkz.config.LoginConfig;
import com.example.hm_dtfkz.databinding.BillRunActivityBinding;
import com.example.hm_dtfkz.entity.BillTypeEntity;
import com.example.hm_dtfkz.litepal.WXLogin;
import com.example.hm_dtfkz.ui.activity.AllBillActivity;
import com.example.hm_dtfkz.ui.activity.SettingActivity;
import com.example.hm_dtfkz.ui.activity.SplashActivity;
import com.example.hm_dtfkz.ui.activity.WebViewActivity;
import com.example.hm_dtfkz.ui.adapter.BillRunAdapter;
import com.example.hm_dtfkz.util.DQLog;
import com.google.gson.Gson;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 壁纸
 */
public class BillRunFragment extends VBBaseFragment {
    private BillRunActivityBinding billRunActivityBinding;
    private List<BillTypeEntity> billEntityList = new ArrayList<>();
    private StaggeredGridLayoutManager staggeredGridLayoutManager=new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);

    @Override
    protected ViewBinding getViewBinding(LayoutInflater inflater) {
        return billRunActivityBinding = BillRunActivityBinding.inflate(inflater);
    }


    public static BillRunFragment createFragment() {
        return new BillRunFragment();
    }

    @Override
    public void initData() {
        super.initData();
        billRunActivityBinding.rrBill.setLayoutManager(staggeredGridLayoutManager);
        BillRunAdapter billRunAdapter =new BillRunAdapter();


        RxhttpUtil.getInstance().get("http://service.picasso.adesk.com/v1/vertical/category",new HashMap<>(), getActivity(), new RxhttpUtil.RxHttpCallBack() {
            @Override
            public void onStart() {

            }

            @Override
            public void onSuccess(String response) {
                DQLog.e("RxhttpUtil==200" + response);

                try {
                    JSONObject jsonObject = new JSONObject(response);
                    String data = jsonObject.getString("res");
                    BillTypeEntity qaIncomeResp = new Gson().fromJson(data, BillTypeEntity.class);
                    billEntityList=qaIncomeResp.getCategory();
                    billRunAdapter.setNewInstance(billEntityList);
                    billRunActivityBinding.rrBill.setAdapter(billRunAdapter);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }


            }

            @Override
            public void onError(String error) {
                DQLog.e("RxhttpUtil==error" + error);

            }

            @Override
            public void onFinish() {

            }
        });
       Intent intent= new Intent();
        billRunAdapter.setOnItemClickLister(new BillRunAdapter.OnItemClickLister() {
            @Override
            public void onItemClick(int position, ImageView tvAnswer) {
                intent.putExtra("type",billEntityList.get(position).getId());
                intent.setClass(getActivity(), AllBillActivity.class);
                startActivity(intent);
            }

        });

        billRunActivityBinding.imgSetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SettingActivity.startActivity(activity);
            }
        });
    }

}
